package co.yixiang.modules.system.rest;

import lombok.AllArgsConstructor;
import cn.hutool.core.convert.Convert;
import co.yixiang.logging.aop.log.Log;
import co.yixiang.modules.system.domain.Appraisal;
import co.yixiang.modules.system.service.AppraisalService;
import co.yixiang.modules.system.dto.queryCriteria.AppraisalQueryCriteria;
import co.yixiang.modules.system.dto.AppraisalDto;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* @author zf
* @date 2021-07-10
*/
@AllArgsConstructor
@Api(tags = "评估价管理")
@RestController
@RequestMapping("/api/appraisal")
public class AppraisalController {

    private final AppraisalService appraisalService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','appraisal:list')")
    public void download(HttpServletResponse response, AppraisalQueryCriteria criteria) throws IOException {
        appraisalService.download(Convert.toList(AppraisalDto.class, appraisalService.queryAll(criteria)), response);
    }

    @GetMapping
    @Log("查询评估价")
    @ApiOperation("查询评估价")
    @PreAuthorize("@el.check('admin','appraisal:list')")
    public ResponseEntity<Object> getAppraisals(AppraisalQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(appraisalService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增评估价")
    @ApiOperation("新增评估价")
    @PreAuthorize("@el.check('admin','appraisal:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody Appraisal resources){
        return new ResponseEntity<>(appraisalService.save(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改评估价")
    @ApiOperation("修改评估价")
    @PreAuthorize("@el.check('admin','appraisal:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody Appraisal resources){
        appraisalService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除评估价")
    @ApiOperation("删除评估价")
    @PreAuthorize("@el.check('admin','appraisal:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody List<Integer> ids) {
        appraisalService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}